package com.cd.learn.cdlearn.resttemplate;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

@Aspect
@Configuration
public class AuditLogAspect {


    @Autowired
    @Qualifier("auditLogRestTemplate")
    private RestTemplate restTemplate;


    /**
     * 拦截操作日志
     */
    @Around("@annotation(auditLog)")
    public Object loggingAuditLog(ProceedingJoinPoint joinPoint, AuditLog auditLog) throws Throwable {
        send();
        return null;
    }

    @Async
    public void send() {

        String uri = "http://127.0.0.1:33888/cdLearn/receive";
        URI uri1 = null;
        try {
            uri1 = new URI(uri);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        RequestEntity<Object> objectRequestEntity = new RequestEntity<>(HttpMethod.GET, uri1);
        restTemplate
                .exchange(uri1, HttpMethod.GET, objectRequestEntity, Object.class);
    }

}
